package eu.flexblog.core.view
{
	import eu.flexblog.core.ApplicationFacade;
	import eu.flexblog.core.interfaces.IFBModule;
	import eu.flexblog.core.model.ModuleRegistryProxy;
	import eu.flexblog.core.view.components.MenuBar;
	import eu.flexblog.modules.users.view.components.Users;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	public class MenuBarMediator extends Mediator
	{
		
		public static const NAME:String = 'MenuBarMediator';
		
		private var moduleRegistry:ModuleRegistryProxy;
		
		public function MenuBarMediator(viewComponent:MenuBar)
		{
			super(NAME, viewComponent);
			
		}
		
		override public function listNotificationInterests():Array
		{
			return [ 
					ApplicationFacade.LOAD_MODULES,
					ApplicationFacade.ADD_MODULE					 
			       ];
		}
		
		
		
		override public function handleNotification(note:INotification):void
		{
			switch ( note.getName() )
			{
				case ApplicationFacade.LOAD_MODULES:
					this.loadModules();
					break;
					
				case ApplicationFacade.ADD_MODULE:
					var module:IFBModule = note.getBody() as IFBModule;
					var menu:Array = module.getMenu();
					
					menuBar.MainMenu.dataProvider = menu;
					break;	
					
				//case ApplicationFacade.REMOVE_COMP:
				//	widgetCanvas.removeChild( note.getBody() as DisplayObject );
				//	break;
			}
		}
		
		protected function get menuBar():MenuBar
		{
			return viewComponent as MenuBar;
		}
		
		private function loadModules():void
		{
			moduleRegistry = facade.retrieveProxy(ModuleRegistryProxy.NAME) as ModuleRegistryProxy;
			var modules:Array = moduleRegistry.modules;
			
			var contentShell:ContentShellMediator = facade.retrieveMediator(ContentShellMediator.NAME) as ContentShellMediator;
			
			
			var users:IFBModule = new eu.flexblog.modules.users.view.components.Users();
			
			users.setModuleShell(contentShell.contentShell);			
			
			sendNotification(ApplicationFacade.ADD_MODULE, users);			
			sendNotification(ApplicationFacade.SHOW_APP);			
					
		}
	}
}